package com.android.thought.badboys.Setting;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;

import com.android.thought.badboys.BadBoysApplication;
import com.android.thought.badboys.Model;
import com.android.thought.badboys.R;
import com.android.thought.badboys.SharedPreferenceUtil;
import com.android.thought.badboys.Info.PhoneInfo;

public class SettingActivity extends Activity implements OnClickListener{
	
	CheckBox mCheckBox = null;
	Model mModel = null;
	
	boolean mPast_MessageAuto = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting);
		
		mCheckBox = (CheckBox) this.findViewById(R.id.checkbox_auto_message);
		
		SharedPreferenceUtil.getInstance(this);
		mPast_MessageAuto = SharedPreferenceUtil.getValue(SharedPreferenceUtil.PREF_INTRO_USER_AUTO_MESSAGE, mPast_MessageAuto);
		
		mCheckBox.setChecked(mPast_MessageAuto);
		
		this.mCheckBox.setOnClickListener(this);
		Application applicaion = getApplication();
		
		if ( applicaion != null && applicaion instanceof BadBoysApplication ) { 
			mModel = ((BadBoysApplication) applicaion).getModel();
		}
		
	}
	
	
	
	@Override
	public void onClick(View v) {
		
		final int id = v.getId();
		
		if ( id == R.id.checkbox_auto_message ) { 
			SharedPreferenceUtil.put(SharedPreferenceUtil.PREF_INTRO_USER_AUTO_MESSAGE, mCheckBox.isChecked());
			return;
		}
		
		
		/*
		int value = 100;
		
		if ( id == R.id.test1 || id == R.id.test4) { 
			value = 100; 
		} else if ( id == R.id.test2 || id == R.id.test5) {
			value = 101; 
		}else if ( id == R.id.test3 || id == R.id.test6) {
			value = 102; 
		}
		
		
		if ( id == R.id.test1 || id == R.id.test2 || id == R.id.test3 )
			mAlarmServices.onRegister(value);
		else mAlarmServices.onDelete(value);
		*/
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		boolean mIsCheking = mCheckBox.isChecked();
		SharedPreferenceUtil.put(SharedPreferenceUtil.PREF_INTRO_USER_AUTO_MESSAGE, mIsCheking);
		if ( mIsCheking != mPast_MessageAuto ) {
			try {
				ArrayList<PhoneInfo> infos = mModel.getAdministrationPhoneInfo().getList();
				
				if ( infos != null && infos.size() > 0 && mIsCheking ) {
					Model.getAlarmServicesInstance(this).onRegister(infos);
				} else {
					Model.getAlarmServicesInstance(this).onDelete();
				}
			
			} catch (Exception e) { }
		}
	}
}
